using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace cb0tServer
{
    class DefaultWebpage
    {
        public static void Check()
        {
            try
            {
                if (!Directory.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website"))
                {
                    Directory.CreateDirectory(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website");
                    BuildGradient();
                    BuildStyle();
                    BuildIndex();
                    BuildReadme();
                }
            }
            catch { }
        }

        private static void BuildGradient()
        {
            Bitmap b = new Bitmap(10, 500);
            Graphics g = Graphics.FromImage(b);
            Rectangle r = new Rectangle(0, 0, 10, 500);
            LinearGradientBrush l = new LinearGradientBrush(r, Color.Black, Color.DimGray, 90f);
            g.FillRectangle(l, r);
            b.Save(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website/gradient.jpg", ImageFormat.Jpeg);
            l.Dispose();
        }

        private static void BuildStyle()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("body");
            builder.AppendLine("{");
            builder.AppendLine("    background-color: #696969;");
            builder.AppendLine("    background-image: url(gradient.jpg);");
            builder.AppendLine("    background-repeat: repeat-x;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("p.title");
            builder.AppendLine("{");
            builder.AppendLine("    font-family: Verdana;");
            builder.AppendLine("    font-size: 20px;");
            builder.AppendLine("    font-weight: bold;");
            builder.AppendLine("    color: #009966;");
            builder.AppendLine("    text-align: center;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("a, p.hashlink");
            builder.AppendLine("{");
            builder.AppendLine("    font-family: Verdana;");
            builder.AppendLine("    font-size: 12px;");
            builder.AppendLine("    color: #FF0101;");
            builder.AppendLine("    text-align: center;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("p.users");
            builder.AppendLine("{");
            builder.AppendLine("    padding-top: 10px;");
            builder.AppendLine("    font-family: Arial;");
            builder.AppendLine("    font-size: 12px;");
            builder.AppendLine("    font-weight: bold;");
            builder.AppendLine("    color: #13E4EB;");
            builder.AppendLine("    text-align: center;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("table.userlist");
            builder.AppendLine("{");
            builder.AppendLine("    border-collapse: collapse;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("td.userlist_item");
            builder.AppendLine("{");
            builder.AppendLine("    font-family: Verdana;");
            builder.AppendLine("    font-size: 10px;");
            builder.AppendLine("    color: #000000;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("div.userlist_container");
            builder.AppendLine("{");
            builder.AppendLine("    border-style: solid;");
            builder.AppendLine("    border-width: 1px;");
            builder.AppendLine("    border-color: #13E4EB;");
            builder.AppendLine("    width: 150px;");
            builder.AppendLine("    height: 200px;");
            builder.AppendLine("    overflow: scroll;");
            builder.AppendLine("    padding-left: 2px;");
            builder.AppendLine("    margin-left: auto;");
            builder.AppendLine("    margin-right: auto;");
            builder.AppendLine("    background-color: #EEEEEE;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("div.foot");
            builder.AppendLine("{");
            builder.AppendLine("    padding-top: 10px;");
            builder.AppendLine("    font-family: Verdana;");
            builder.AppendLine("    font-size: 10px;");
            builder.AppendLine("    color: #09F623;");
            builder.AppendLine("    text-align: center;");
            builder.AppendLine("    margin-left: auto;");
            builder.AppendLine("    margin-right: auto;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("div.middle");
            builder.AppendLine("{");
            builder.AppendLine("    border-style: solid;");
            builder.AppendLine("    border-width: 1px;");
            builder.AppendLine("    border-color: #13E4EB;");
            builder.AppendLine("    width: 300px;");
            builder.AppendLine("    padding-bottom: 20px;");
            builder.AppendLine("    margin-left: auto;");
            builder.AppendLine("    margin-right: auto;");
            builder.AppendLine("    background-color: #555555;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("div.quote");
            builder.AppendLine("{");
            builder.AppendLine("    font-family: Verdana;");
            builder.AppendLine("    font-size: 11px;");
            builder.AppendLine("    color: #13E4EB;");
            builder.AppendLine("    text-align: center;");
            builder.AppendLine("    padding-top: 11px;");
            builder.AppendLine("}");
            builder.AppendLine("");
            builder.AppendLine("span.quote_text");
            builder.AppendLine("{");
            builder.AppendLine("    color: #FF0000;");
            builder.AppendLine("    font-size: 14px;");
            builder.AppendLine("}");
            File.WriteAllText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website/style.css", builder.ToString(), Encoding.Default);
        }

        private static void BuildReadme()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("sb0t website notes");
            builder.AppendLine("==================");
            builder.AppendLine("");
            builder.AppendLine("The following file extensions have been supported and tested:");
            builder.AppendLine("");
            builder.AppendLine(".gif");
            builder.AppendLine(".jpg");
            builder.AppendLine(".bmp");
            builder.AppendLine(".png");
            builder.AppendLine(".html");
            builder.AppendLine(".htm");
            builder.AppendLine(".txt");
            builder.AppendLine(".css");
            builder.AppendLine(".js");
            builder.AppendLine("");
            builder.AppendLine("Other file types may be supported but not tested.");
            builder.AppendLine("");
            builder.AppendLine("Identifiers are available to include chatroom information on html");
            builder.AppendLine("pages which have had their extension changed to .sb0t.  The following");
            builder.AppendLine("identifiers are currently available:");
            builder.AppendLine("");
            builder.AppendLine("%ip  ->  Website visitor's IP address");
            builder.AppendLine("%userlist  ->  current userlist (use pre-named css classes for styling)");
            builder.AppendLine("%roomname  ->  chatroom name");
            builder.AppendLine("%hashlink  ->  chatroom hashlink (when available)");
            builder.AppendLine("%uptime  ->  chatroom uptime");
            builder.AppendLine("%lastseen  ->  website visitor's last known chatroom username (when available)");
            builder.AppendLine("%hostnames  ->  list of host users currently logged in");
            builder.AppendLine("%usercount  ->  current chatroom user count");
            builder.AppendLine("%admincount  ->  current chatroom admin count");
            builder.AppendLine("%roomlocation  ->  chatroom physical location (when available)");
            builder.AppendLine("%hoststatus  ->  current room information host status");
            builder.AppendLine("%randomquote  ->  a random quote (when available)");
            builder.AppendLine("%hitcount  ->  website hit counter");
            File.WriteAllText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website/readme.txt", builder.ToString(), Encoding.Default);
        }

        private static void BuildIndex()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<!DOCTYPE html");
            builder.AppendLine("PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
            builder.AppendLine("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
            builder.AppendLine("");
            builder.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            builder.AppendLine("");
            builder.AppendLine("<head>");
            builder.AppendLine("");
            builder.AppendLine("  <title>%roomname</title>");
            builder.AppendLine("  <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />");
            builder.AppendLine("");
            builder.AppendLine("</head>");
            builder.AppendLine("");
            builder.AppendLine("<body>");
            builder.AppendLine("");
            builder.AppendLine("<div>");
            builder.AppendLine("  <p class=\"title\">Welcome to the %roomname webpage %lastseen :-)</p>");
            builder.AppendLine("  <p class=\"hashlink\"><a href=\"arlnk://%hashlink\">Click here to join %roomname !!</a></p>");
            builder.AppendLine("</div>");
            builder.AppendLine("");
            builder.AppendLine("<div class=\"middle\">");
            builder.AppendLine("  <p class=\"users\">people currently in %roomname</p>");
            builder.AppendLine("  <div class=\"userlist_container\">");
            builder.AppendLine("%userlist");
            builder.AppendLine("  </div>");
            builder.AppendLine("</div>");
            builder.AppendLine("");
            builder.AppendLine("<div class=\"quote\">");
            builder.AppendLine("  random quote:<br />");
            builder.AppendLine("  <span class=\"quote_text\">%randomquote</span>");
            builder.AppendLine("</div>");
            builder.AppendLine("");
            builder.AppendLine("<div class=\"foot\">");
            builder.AppendLine("  Current hosts: %hostnames<br />");
            builder.AppendLine("  Current user count: %usercount<br />");
            builder.AppendLine("  Current admin count: %admincount<br />");
            builder.AppendLine("  Server uptime: %uptime<br />");
            builder.AppendLine("  Server location: %roomlocation<br />");
            builder.AppendLine("  Host status: %hoststatus<br />");
            builder.AppendLine("  You are this webpages visitor number <b>%hitcounter</b>");
            builder.AppendLine("</div>");
            builder.AppendLine("");
            builder.AppendLine("</body>");
            builder.AppendLine("");
            builder.AppendLine("</html>");
            File.WriteAllText(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "website/index.sb0t", builder.ToString(), Encoding.Default);
        }
    }
}
